﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./GeneralSetting.aspx";
    public string strCondition = "";
    public int NumOfItemsPerPage = 20;
    public string strPager = "";
    public string strIDs = "";
    public string strError = "";
    public string strMess = "";
    public int nPermissions = 0;
    public int nPage = 1;
    public string strPageID = "page_number";
    protected void Page_Load(object sender, EventArgs e)
    {

        int nPage = 1;
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;

        if ((Session["logined_id"] != null) && (int.Parse(Session["logined_id"].ToString()) > 0))
        {
            if (Session["GeneralSetting"] != "") nPermissions = int.Parse(Session["GeneralSetting"].ToString());
            if (nPermissions < 1) Response.Redirect("Login.aspx");
        }
        else
            Response.Redirect("Login.aspx");

        if (!Page.IsPostBack)
        {
            if (Request.QueryString["action"] == "delete" && nPermissions > 7)
            {
                GeneralSetting obj = new GeneralSetting();
                obj.ID = long.Parse(Request.QueryString["ID"].ToString());
                obj.Delete(GV.cn, GV.trans);
                Response.Redirect("GeneralSetting.aspx");
            }
            if (Request.QueryString["action"] == "edit")
                txtCode.ReadOnly = false;
            else
                txtCode.ReadOnly = true;
            if (Request.QueryString["ID"] != null)
            {
                loadGeneralSetting();
            }
            #region Build List
            int nNumOfPages = 0;

            DataTable dtList;
            BuildList(strCondition, NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, null, null);
            if (nPage > nNumOfPages) nPage = 1;

            rptData.DataSource = dtList;
            rptData.DataBind();
            #endregion
            #region Build Pager
            CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
            #endregion
        }

    }
    protected void loadGeneralSetting()
    {
        GeneralSetting obj = new GeneralSetting();
        obj.ID = long.Parse(Request.QueryString["ID"]);
        obj.Load(GV.cn, GV.trans);
        txtName.Text = obj.Name;
        txtCode.Text = obj.Code;
        txtValue.Text = obj.Value;
        txtDescription.Text = obj.Description;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        strError = "";
        strMess = "";
        GeneralSetting obj = new GeneralSetting();
        if (Request.QueryString["ID"] != null)
        {
            obj.ID = long.Parse(Request.QueryString["ID"]);
            obj.Load(GV.cn, GV.trans);
        }
        obj.Name = txtName.Text.Trim();
        obj.Code = txtCode.Text.Trim();
        obj.Value = txtValue.Text.Trim();
        obj.Description = txtDescription.Text.Trim();
        if (obj.Name.Length == 0)
        {
            lblError.Text = "Name can not be blank.";
            return;
        }
        if (obj.Code.Length == 0)
        {
            obj.Code = obj.Name;
        }
        if (GeneralSetting.isExisting(obj.ID, obj.Code, GV.cn, GV.trans) > 0)
        {
            lblError.Text = "Code '" + obj.Code + "' is Existing.";
            return;
        }
        obj.Save(GV.cn, GV.trans);
        Response.Redirect("GeneralSetting.aspx");
    }
    private void BuildList(string strCondition, int NumOfItemsPerPage, int PageNo, out int nNumOfPages, out DataTable dtList, MySqlConnection cn, MySqlTransaction trans)
    {
        DataSet ds = null;
        dtList = new DataTable();
        dtList.Columns.Add("Index");
        dtList.Columns.Add("GName");
        dtList.Columns.Add("GCode");
        dtList.Columns.Add("GValue");
        dtList.Columns.Add("GDescription");
        dtList.Columns.Add("Action");
        nNumOfPages = 0;
        GeneralSetting.GetList(strCondition, "Code", NumOfItemsPerPage, PageNo, out nNumOfPages, out ds, GV.cn, GV.trans);
        if (ds != null)
        {
            int count = 0;
            if (Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "")
            {
                int pagenumber = 1;
                try
                {
                    pagenumber = int.Parse(Request.QueryString["page_number"]);
                    count = NumOfItemsPerPage * (pagenumber - 1);
                }
                catch { count = 0; }
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                count++;
                string strIndex = count.ToString();
                string GName = dr["Name"].ToString();
                string GCode = dr["Code"].ToString();
                string GValue = dr["Value"].ToString();
                if (GValue.Length > 0)
                {
                    try
                    {
                        GValue = double.Parse(GValue).ToString("#,0.##");
                    }
                    catch { }
                }
                string GDescription = dr["Description"].ToString();
                string strAction = "";
                if (nPermissions >= 3)
                    strAction = "<a href=\"GeneralSetting.aspx?ID=" + dr["ID"].ToString() + ((Request.QueryString["page_number"] != null && Request.QueryString["page_number"].ToString() != "") ? "&page_number=" + Request.QueryString["page_number"].ToString() : "") +
                    "\">Edit</a>";
                if (nPermissions > 7)
                {
                    //strAction += " - <a href=\"javascript:doDelete('" + System.Web.HttpUtility.HtmlEncode(dr["ID"].ToString()) + "','" + System.Web.HttpUtility.HtmlEncode(GCode) + "')\">" + "Delete</a>";
                }

                string[] strValues = new string[] {  strIndex, 
                            GName,
                            GCode,
                            GValue,
                            GDescription,
                            strAction };
                dtList.Rows.Add(strValues);
            }
        }
    }
}
